package com.gxc.datacenter.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.gxc.datacore.metadata.domain.Column;
import com.gxc.datacore.metadata.domain.Table;
import com.gxc.datacore.metadata.service.ColumnService;
import com.gxc.datacore.metadata.service.TableService;
import com.gxc.datacore.metadata.util.CollectUtil;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;
import com.gxc.webcore.util.JsonUtil;
import com.gxc.webcore.util.MapUtil;
import com.gxc.webcore.util.RequestUtil;
import com.gxc.webcore.util.StringUtil;

@Controller("datacenter/columnController")
public class ColumnController {

	@Autowired
	private TableService tableService;
	@Autowired
	private ColumnService columnService;

	public ModelAndView listByTable(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("datacenter/manage/column_listByTable.vm", null);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView gridByTable(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map params = RequestUtil.getInMap(request);
		PageInfo pageInfo = PageInfo.getFromRequest(request);
		PageList pageList = columnService.listPage(params, pageInfo);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", pageList);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 采集表对应列信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView collocetByTable(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String database_id = request.getParameter("database_id");
		String table_id = request.getParameter("table_id");
		Table table = tableService.retrieve(table_id);
		List list = CollectUtil.getColumns(database_id, table.getSchema_name(),
				table.getTable_name());
		List columnlist = MapUtil.maplist2beanlist(list, Column.class);
		for (Column column : (List<Column>) columnlist) {
			try {
				Column temp = columnService.retrieveByName(table_id,
						column.getColumn_name());
				if (temp != null) {
					column.setId(temp.getId());
					columnService.update(column);
				} else {
					column.setId("column_" + StringUtil.getUUID());
					columnService.create(column);
				}
				columnlist.add(table);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", columnlist);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

}
